home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Demos / A.D. Software / OOFILE / Buildable, limited OOFILE / samples / ooftst20.cpp < prev    next >
C/C++ Source or Header  |  1996-03-21  |  3KB  |  126 lines

  1. // Copyright 1994 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST20
  4.  
  5. // This sample demonstrates how to create an ad-hoc query.
  6.  
  7. // Simple stream I/O is used to interact with the user.
  8.  
  9. #include "oofile.hpp"
  10. #include "ooftst01.inc"
  11.  
  12. void anyName(const char* searchStrings[], bool isOr, unsigned short numSch);
  13. void lastName(const char* searchStrings[], unsigned short numSch);
  14.  
  15. #define numEntries(A) sizeof(A)/sizeof(A[0])
  16.  
  17.     dbConnect_ctree    theDB;
  18.     dbPeople     People;
  19.  
  20.  
  21. int main()
  22. {
  23.     cout << "OOFILE Validation Suite - Test 20\n"
  24.          << "Demonstration of ad-hoc query building\n";
  25.     
  26.     theDB.useSeparateFiles();  // note the blank connection names!
  27. // this test creates People.dat, People.idx & Blobs
  28.  
  29. #ifdef _Macintosh
  30. // this feature only on the Mac at present
  31.     #define kExistsName  ":test01:People.dat"
  32.     #define kDatabaseName "test01"
  33. #else
  34.     #define kExistsName "People.dat"
  35.     #define kDatabaseName ""
  36. #endif    
  37.  
  38.     if (dbConnect::fileExists(kExistsName))
  39.         theDB.openConnection(kDatabaseName);
  40.     else {
  41.         theDB.newConnection(kDatabaseName);
  42.         People.AddTestData();
  43.     }
  44.  
  45.     People.selectAll();
  46.     cout << "Listing records\n" << (dbView(People) << People.LastName << People.OtherNames);
  47.  
  48.     const char* userEntries[] = {"Dent", "Andy"};    
  49.     anyName(userEntries, true, numEntries(userEntries));
  50.     anyName(userEntries, false, numEntries(userEntries));
  51.  
  52.     const char* userEntries2[] = {"Dent", "Tay*"};    
  53.     lastName(userEntries2, numEntries(userEntries));
  54.     cout << "Test Completed" << endl;
  55.     
  56.     return EXIT_SUCCESS;
  57.  
  58.  
  59. void
  60. anyName(const char* searchStrings[], bool isOr, unsigned short numSch)
  61. {
  62.     dbQuery theQuery;
  63.     if (isOr)
  64.         cout << "OR";
  65.     else
  66.         cout << "AND";
  67.     cout << " Querying either Last or Othernames = \n";
  68.     
  69.     for (unsigned short i=0; i<numSch; i++) {
  70.         cout << "   " << searchStrings[i] << endl;
  71.  
  72. // construct clauses on the heap otherwise they are temporaries
  73. // which won't exist by the time we do the search()
  74.         
  75.         dbQueryClause* term1 = new dbQueryBinary(
  76.             People.LastName == searchStrings[i]
  77.         );
  78.         dbQueryClause* term2 = new dbQueryBinary(
  79.             People.OtherNames == searchStrings[i]
  80.         );
  81.  
  82. // the following is a bit subtle - the operator||
  83. // which takes pointers creates a dbQueryBinaryComboOwner
  84. // which will delete the sub-clauses
  85.         if (isOr) {
  86. //            theQuery |= (*term1 || term2); equivalent to the following
  87.             theQuery |= term1;
  88.             theQuery |= term2;
  89.         }
  90.         else
  91.             theQuery &= (*term1 || term2);
  92.     }
  93.     People.search(theQuery);
  94.  
  95.     cout << "\nListing result of query\n" 
  96.          << (dbView(People) << People.LastName << People.OtherNames) << endl;
  97.  
  98. }
  99.  
  100.  
  101.  
  102. void
  103. lastName(const char* searchStrings[], unsigned short numSch)
  104. {
  105.     dbQuery theQuery;
  106.     cout << "Querying Lastname = any of\n";
  107.     
  108.     for (unsigned short i=0; i<numSch; i++) {
  109.         cout << "   " << searchStrings[i] << endl;
  110.  
  111. // construct clauses on the heap otherwise they are temporaries
  112. // which won't exist by the time we do the search()
  113.         
  114.         dbQueryClause* theTerm = new dbQueryBinary(
  115.             People.LastName == searchStrings[i]
  116.         );
  117.  
  118.         theQuery |= theTerm;
  119.     }
  120.     People.search(theQuery);
  121.  
  122.     cout << "\nListing result of query\n" 
  123.          << (dbView(People) << People.LastName << People.OtherNames) << endl;
  124.  
  125. }